package com.woniu.service.impl;

import com.woniu.outlet.dao.ProSchedulingPoMapper;
import com.woniu.outlet.po.ProSchedulingPo;
import com.woniu.outlet.po.ProSchedulingPoExample;
import com.woniu.outlet.po.WorkingNum;
import com.woniu.service.ProSchedulingPoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProSchedulingPoServiceIml implements ProSchedulingPoService {

    @Autowired
    private ProSchedulingPoMapper proSchedulingPoMapper;

    //查询生产排程列表
    public List<ProSchedulingPo> findAllProShces(ProSchedulingPo po){
        ProSchedulingPoExample poExample = new ProSchedulingPoExample();
        ProSchedulingPoExample.Criteria criteria = poExample.createCriteria();
        if (!StringUtils.isEmpty(po.getId())){
            criteria.andIdEqualTo(po.getId());
        }
        if (!StringUtils.isEmpty(po.getProPlanId())){
            criteria.andIdEqualTo(po.getProPlanId());
        }
        List<ProSchedulingPo> proSchedulingPos = proSchedulingPoMapper.selectByExample(poExample);
        return proSchedulingPos;
    }


    //根据id查询单个生产排程及关联的工序
    public ProSchedulingPo getProScheById(Long id) {
        ProSchedulingPo proSchedulingPo1 = proSchedulingPoMapper.selectByPrimaryKey(id);

        List<ProSchedulingPo> proSchedulingPo = proSchedulingPoMapper.getProScheById(id);
        List<WorkingNum> workingNums = new ArrayList<>();
        if (proSchedulingPo.size() != 0){
            for (ProSchedulingPo po : proSchedulingPo) {
                WorkingNum workingNum = new WorkingNum();
                workingNum.setProNum(po.getProNum());
                workingNum.setWorkingProcedureCode(po.getWorkingProcedureCode());
                workingNum.setProcessName(po.getProcessName());
                workingNums.add(workingNum);
            }
            proSchedulingPo1.setWorkerName(proSchedulingPo.get(0).getWorkerName());
            proSchedulingPo1.setWorkingNumrs(workingNums);
        }
        return proSchedulingPo1;
    }
}
